package com.inspur.demo.config;

import com.netflix.appinfo.InstanceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 *    
 * Eureka事件监听
 *
 * @desc:
 * @author: li.peng02
 * @date: 2019/3/11
 * @version: v1.0 
 */
@Component
public class EurekaEventListener {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @EventListener
    public void listener(EurekaInstanceRegisteredEvent event) {
        InstanceInfo instanceInfo = event.getInstanceInfo();
        String ipAddr = instanceInfo.getIPAddr();
        String instanceId = instanceInfo.getInstanceId();
        logger.info(">>>>>" + instanceId + "注册到Server，IP为" + ipAddr);
    }

    @EventListener
    public void listener(EurekaInstanceRenewedEvent event) {
        InstanceInfo instanceInfo = event.getInstanceInfo();
        String instanceId = instanceInfo.getInstanceId();
        logger.info(">>>>>" + instanceId + "向Server续约");
    }

    @EventListener
    public void listener(EurekaRegistryAvailableEvent event) {
        logger.info(">>>>>注册中心启动事件触发");
    }

    @EventListener
    public void listener(EurekaServerStartedEvent event) {
        logger.info(">>>>>EurekaServer启动事件触发");
    }

    @EventListener
    public void listener(EurekaInstanceCanceledEvent event) {
        String serverId = event.getServerId();
        logger.info(">>>>>" + serverId + "下线");
    }
}
